﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Gateway.Host
{
    public class SendSpecialTask : Task
    {
        protected IServiceConfigProvider _configProvider;
        protected IPushMessageProvider _pushProvider;

        public SendSpecialTask(IServiceConfigProvider provider,IPushMessageProvider pushProvider)
        {
            _configProvider = provider;
            _pushProvider = pushProvider;
        }

        protected override int TaskEnable
        {
            get
            {
                return _configProvider.SendSpeicialTaskEnable;
            }
            set
            {
                TaskEnable = value;
            }
        }

        protected override int TaskSpan
        {
            get
            {
                return _configProvider.SendSpeicialTaskSpan;
            }
            set
            {
                base.TaskSpan = value;
            }
        }

        private string Recipients
        {
            get { return _configProvider.SendSpeicialTaskRecipients; }
        }

        private string Times
        {
            get { return _configProvider.SendSpeicialTaskTimes; }
        }

        private int timeindex = 0;
        private DateTime prevTime;

        private Nullable<DateTime> ReminderTime
        {
            get
            {
                if (string.IsNullOrEmpty(Times)) return null;
                string[] times = Times.Split(';');
                try
                {
                    if (timeindex >= times.Length) timeindex = 0;
                    if (DateTime.Parse(times[timeindex]) > prevTime)
                        return DateTime.Parse(times[timeindex]);
                    else
                        return DateTime.Parse(times[timeindex]).AddDays(1);
                }
                catch
                {
                    return null;
                }
            }
        }

        private void CallNext(){
            timeindex++;
        }

        private void InitTimeindex()
        {
            try
            {
                DateTime now = DateTime.Now;
                DBLog.Debug("Times : " + Times);
                string[] times = Times.Split(';');
                foreach (string dts in times)
                {
                    DateTime dt = DateTime.Parse(dts);
                    if (now > dt)
                    {
                        timeindex++;
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
                if (timeindex >= times.Length) {
                    DBLog.Debug("prevTime : " + DateTime.Parse(times[timeindex - 1]));
                    prevTime = DateTime.Parse(times[timeindex - 1]);
                }
            }
            catch (Exception ex) { DBLog.Error(ex.ToString()); }
        }

        protected override void ExecuteCore()
        {
            InitTimeindex();
            DBLog.Debug("Recipients : " + Recipients);
            while (_scanAble && TaskEnable == 1)
            {
                try
                {
                    Nullable<DateTime> rt = ReminderTime;
                    if(rt != null)
                        DBLog.Debug("Next Time is : " + rt.ToString());
                    if (rt != null && DateTime.Now > rt)
                    {
                        DBLog.Debug("Send to : " + Recipients + " at " + DateTime.Now.ToString());
                        int result = _pushProvider.SendSMS("Admin", "Admin", "客服中心短信系统测试 [" + DateTime.Now + "]", Recipients);
                        DBLog.Debug("result : " + result.ToString());
                        prevTime = (DateTime)rt;
                        CallNext();
                    }
                }
                catch (Exception ex) { DBLog.Error(ex.ToString()); }
                System.Threading.Thread.Sleep(
                    TaskSpan < GlobalFactory.TaskSpanBorder ? GlobalFactory.TaskSpanBorder : TaskSpan);
            }
        }
    }
}
